<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class MemberCommentResource extends JsonResource
{
    public function toArray($request)
    {
        $data = [
            'id'           => $this->id,
            'content'      => $this->content,
            'type'         => $this->type,
            'status'       => $this->status,
            'member'       => [
                'id'       => $this->member->id,
                'avatar'   => $this->member->avatar,
                'nickname' => $this->member->nickname,
            ],
            'created_at'   => date('m月d日 H:i', strtotime($this->created_at)),
            'created_time' => strtotime($this->created_at),
        ];

        if ($this->type == 1) {
            return array_merge($data, [
                'article_id'      => $this->article_id,
                'article_content' => $this->article->type == 1 ? Str::limit($this->article->content, 30) : $this->article->desc,
                'publisher'       => [
                    'publisher_id'     => $this->article->publisher->id,
                    'publisher_name'   => $this->article->publisher->name,
                    'publisher_avatar' => $this->article->publisher->avatar,
                ]
            ]);
        } elseif ($this->type == 2) {
            return array_merge($data, [
                'topic_id'      => $this->topic_id,
                'topic_content' => Str::limit($this->topic->content, 30),
                'author'        => [
                    'author_id'     => $this->topic->member->id,
                    'author_name'   => $this->topic->member->nickname,
                    'author_avatar' => $this->topic->member->avatar,
                ]
            ]);
        } elseif ($this->type == 3) {
            return array_merge($data, [
                'course_id'    => $this->course->id,
                'course_cover' => $this->course->cover,
                'course_name'  => $this->course->name,
            ]);
        } else {
            return [];
        }
    }
}
